#!/usr/bin/perl
use strict;
use warnings;

my %hInit;
my @aInit;
while(<DATA>){
  chomp;
  s/\s*;\s*$//;
  my($sKey,@aData) = split /\s*[=_]\s*/;
  $hInit{$sKey} = join " ",@aData;
  push @aInit, $sKey;
}

sub get_init{
  my ($sKey) = @_;
  return $sKey if(!exists $hInit{$sKey});
  while($hInit{$sKey} =~ m/\b(\w+[^0 ])\b/){
    $hInit{$sKey} =~ s/\b(\w+[^0 ])\b/get_init($1)/ge;
  }  
  return $hInit{$sKey};
}
foreach my $sKey(keys %hInit){
  $hInit{$sKey} = get_init($sKey);
}
foreach (@aInit){
   $hInit{$_} =~ s/ /_/g;
   print "$_ = $hInit{$_};\n";
}
__DATA__
ABC =  A1_B1;
ABD =  C1_D1;
A1    =  D0_A0;
B1    =  A1_ABD;
C1    =  C0;
D1    =  B0_C1;
